from random import randint
from time import sleep
from func import print_health

while 1:
    gamemode = input('请选择游戏模式 (简单/困难)')

    if gamemode == '简单' or gamemode == '困难':
        break

print('——————————游戏开始——————————')

button_num = randint(1, 5)
button_ls = []

is_your_round = randint(0, 1) == 1
skip_round = False
player_health_max = 5
enemy_health_max = 5

player_health, enemy_health = player_health_max, enemy_health_max

while 1:
    if player_health == 0 or enemy_health == 0:
        if player_health == 0:
            print("你输了，游戏结束！")
        if enemy_health == 0:
            print("你赢了，游戏结束！")

    else:
        if not button_ls:
            print('补充中……')

            sleep(1)

            button_num = randint(1, 5)
            button_ls = [True] * button_num

            for i in range(0, len(button_ls)):
                button_ls[i] = True if randint(0, 1) == 1 else False

            prls = button_ls
            prls.sort()

            for i in prls:
                if i:
                    print('◉', end=' ')
                else:
                    print('○', end=' ')
            print()

        if is_your_round:
            print("轮到你了")
            button_index = randint(0, len(button_ls) - 1)
            button_kind = button_ls.pop(button_index)

            while 1:
                ans = input('射向自己输入1，射向对方输入2\n')

                if not (ans == '1' or ans == '2'):
                    print('请正确输入')
                    continue
                break
            print('你扣下了扳机……')
            sleep(0.5)
            if ans == '1':
                if button_kind:
                    player_health -= 1
                    print(f"嘭！你掉了一点血，剩余{player_health}滴血！")
                else:
                    print(f'是空的……还好……')
                    skip_round = True
            else:
                if button_kind:
                    enemy_health -= 1
                    print(f"嘭！他掉了一点血，他剩余{enemy_health}滴血！")
                else:
                    print(f'是空的……可惜……')
        else:
            button_index = randint(0, len(button_ls) - 1)
            button_kind = button_ls.pop(button_index)
            print("敌人正在决定")
            if gamemode == '简单':
                ans = randint(1, 2)
            elif gamemode == '困难':
                print(f'困难模式，正在决定，实弹为{button_ls.count(True)}个，空弹为{button_ls.count(False)}')
                if button_ls.count(True) > button_ls.count(False):
                    ans = 2
                elif button_ls.count(True) < button_ls.count(False):
                    ans = 1
                else:
                    ans = randint(1, 2)
            else:
                raise ValueError(f'variable \'gamemode\'should be \'简单\' or \'困难\', but it is \'{gamemode}\'')
            sleep(randint(50, 150) / 100)
            if ans == 2:
                print("他决定对你射击")
                if button_kind:
                    player_health -= 1
                    print(f"嘭！你掉了一点血，剩余{player_health}滴血！")
                else:
                    print(f'是空的……还好……')
            else:
                print("他决定对自己射击")
                if button_kind:
                    enemy_health -= 1
                    print(f"嘭！他掉了一点血，他剩余{enemy_health}滴血！")
                else:
                    print(f'是空的……可惜……')
                    skip_round = True
        print_health(player_health, enemy_health, player_health_max, enemy_health_max)
        if not skip_round:
            is_your_round = not is_your_round
            skip_round = False

        prls = button_ls
        prls.sort()

        for i in prls:
            if i:
                print('◉', end=' ')
            else:
                print('○', end=' ')
        print()
